/**
 * Created with IntelliJ IDEA.
 * User: aleksandr
 * Date: 12.07.13
 */
package com.google.code.bankagregator.dao;

import com.google.code.bankagregator.entities.ScheduleEntity;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.util.logging.Level;
import java.util.logging.Logger;

@Stateless
public class ScheduleDAO extends DAO {
    private final Logger log = Logger.getLogger(ScheduleDAO.class.getName());

    public void create(ScheduleEntity entity) {
        getEntityManager().merge(entity);
    }

    public ScheduleEntity read(String paramName) {
        Query query = getEntityManager()
                .createQuery("SELECT t FROM ScheduleEntity t WHERE t.paramName = :paramName");
        query.setParameter("paramName", paramName);
        try {
            return (ScheduleEntity) query.getSingleResult();
        } catch (NoResultException ex) {
            log.log(Level.INFO, "Config parameter " + paramName + " not found at database.");
        }
        return null;
    }

    public void update(ScheduleEntity entity) {
        getEntityManager().merge(entity);
    }

    public void delete(ScheduleEntity entity) {
        getEntityManager().remove(entity);
    }
}
